﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace TI
{
    public class LogCatalogItem
    {
        public string FolderName { get; set; }
        
        /// <summary>
        /// Список файлов каталога
        /// </summary>
        public List<string> FileList { get; set; }

        public List<string> ZipFileList { get; set; }

        public List<string> OkFileList { get; set; }

        public List<string> EmptyFileList { get; set; }

        public List<string> ErrFileList { get; set; }

        public List<string> ErrScripts { get; set; }

        public List<string> ErrZipFileList { get; set; }

        Func< List<string>,string, bool> fncAddLstItem =delegate (List<string>  lst, string f){
          if (null == lst)
                lst = new List<string>();
            lst.Add(f);
        return true ;
    };

        public void AddFile(string f){
            if (null == this.FileList )
                FileList = new List<string>();
            FileList.Add(f);
        }
        public void AddOkFile(string f)
        {
            if (null == this.OkFileList)
                OkFileList = new List<string>();
            OkFileList.Add(f);
        }


        public void AddEmptyFile(string f)
        {
            if (null == this.EmptyFileList)
                EmptyFileList = new List<string>();
            EmptyFileList.Add(f);
        }

        public void AddErrFile(string f)
        {
            if (null == this.ErrFileList )
                ErrFileList = new List<string>();
            ErrFileList.Add(f);
        }

        public void AddErrScripts(string f)
        {
            if (null == this.ErrScripts )
                ErrScripts = new List<string>();
            ErrScripts.Add(f);
        }

        public void AddErrZipFile(string f)
        {
            if (null == this.ErrZipFileList)
                ErrZipFileList = new List<string>();
            ErrZipFileList.Add(f);
        }


        public void AddZipFile(string f)
        {
            if (null == this.ZipFileList)
                ZipFileList = new List<string>();
            ZipFileList.Add(f);
        }
    }
    
    public class TransportLog
    {
        public List<LogCatalogItem> Folders { get; set; }

        public  LogCatalogItem CurrentFolder
        {
            get
            {
                foreach (LogCatalogItem i in Folders)
                {
                    if (_currentFolder.Equals(i.FolderName))
                        return i;
                }
                return null;
            }
        }

        private string _currentFolder { get; set; }
        //Общая статистика
        public int FileListCnt { get; set; }

        public int OkFileListCnt { get; set; }

        public int EmptyFileListCnt { get; set; }

        public List<string> ErrFileList { get; set; }

        public void  AddFolder(string folderName)
        {
            _currentFolder = folderName;

            if (null == Folders)
                Folders = new List<LogCatalogItem>();

            foreach (LogCatalogItem i in Folders)
            { 
                if(folderName.Equals (i.FolderName))
                    return ;
            }
            Folders.Add(new LogCatalogItem() { FolderName = folderName });
        }

        // сохраниться в папке согласно указанному дню
        public void Save(string folder)
        {
            string filename = folder + LogName + ".xml";
            XmlSerializer mySerializer = new
            XmlSerializer(typeof(TransportLog));
            
            StreamWriter myWriter = new StreamWriter(filename);
            mySerializer.Serialize(myWriter, this);
            myWriter.Close();
        }

        public DateTime Start { get; set; }

        public DateTime Finish { get; set; }

        public static  TransportLog GetTransportLog(string folder)
        {
            FileStream fs = null;
            try
            {
                Helper.FileTools.СeckAndCreateFolder(folder); 
                string filename = folder + LogName + ".xml";
                XmlSerializer serializer = new XmlSerializer(typeof(TransportLog));
                fs = new FileStream(filename, FileMode.Open);
                return (TransportLog)serializer.Deserialize(fs);
            }
            catch
            {
                return new TransportLog();
            }
            finally {
                if (null != fs)
                {
                    fs.Close();
                    fs.Dispose();
                }
            }
        }

        private static  string LogName {
            get
            {
                return DateTime.Now.ToShortDateString().Replace(".", "_"); 
            }
        }
     
    }
}
